import {
    _decorator,
    Component,
    instantiate,
    Material,
    Node,
    Sprite,
    tween,
} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SpriteAblation')
export class SpriteAblation extends Component {
    @property(Sprite)
    sp: Sprite = null;

    @property(Material)
    mat: Material = null!;

    onLoad() {
        // console.log(this.mat['clone']);
        this.sp.customMaterial = this.mat;
    }

    start() {
        this.schedule(
            () => {
                this.dissolver(1);
            },
            2,
            10,
        );
    }

    setValue(value: number) {
        this.sp.material!.setProperty('noiseThreshold', value);
    }

    dissolver(duration: number) {
        const obj = { value: 0 };
        tween(obj)
            .to(
                duration,
                { value: 1 },
                {
                    onUpdate: () => {
                        this.setValue(obj.value);
                    },
                },
            )
            .start();
    }
}
